package com.feng.zhihao.apigeteway.util;

import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.feng.zhihao.apigeteway.entity.InterfaceFieldInfo;
import com.feng.zhihao.apigeteway.entity.InterfaceInfo;
import com.feng.zhihao.apigeteway.gateway.ReqContext;
import com.feng.zhihao.apigeteway.gateway.bean.FIELD_TYPE;
import com.feng.zhihao.apigeteway.gateway.bean.INTERFACE_FIELD_POSITION;
import com.feng.zhihao.apigeteway.gateway.bean.INTERFACE_FIELD_POSITION_RES;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import com.xiaoleilu.hutool.util.StrUtil;

import ognl.Ognl;
import ognl.OgnlException;

public class ParamsUtil {
	static Log log=LogFactory.get();
	/**
	 * 接口参数验证。
	 * @param interfaceInfo  接口对象
	 * @param reqContext 请求上下文
	 * @return
	 */
	public static String validator(InterfaceInfo interfaceInfo,ReqContext reqContext){
		String message = interfaceInfo.getInterfaceFieldInfos()
		.stream()
		.map(i->{
				if(! i.getInterfaceFieldPosition().startsWith("FIELD_POSITION_REQ") ){
					return "";
				}
				Object o = getValue(i, reqContext);
				return validatorField(i, o);
		})
		.collect(Collectors.joining());
		return message;
	}
	/**
	 * 字段验证
	 * @param fieldInfo  字段
	 * @param o 请求值
	 * @return
	 */
	public static String  validatorField(InterfaceFieldInfo fieldInfo,Object o){
		if(o == null){
			if(fieldInfo.getInterfaceFieldIsNull()){
				return StrUtil.format("字段:{}, 不允许为NULL;\r\n", fieldInfo.getInterfaceFieldName());
			}else{
				return "";
			}
		}else{
			FIELD_TYPE field_TYPE=(FIELD_TYPE) BeanUtil.getBean(fieldInfo.getInterfaceFieldType());
			return field_TYPE.validatorField(fieldInfo, o);
		}
	}
	/**
	 * 通过字段信息从请求参数获取值
	 * @param interfaceFieldInfo
	 * @param reqContext
	 * @return
	 */
	public static Object getValue(InterfaceFieldInfo interfaceFieldInfo,ReqContext reqContext){
		if(interfaceFieldInfo == null){
			return null;
		}
		if(interfaceFieldInfo.getInterfaceFieldInfo() !=null){
			Object o = getValue(interfaceFieldInfo.getInterfaceFieldInfo(), reqContext);
			if(o == null){
				return null;
			}
		}
		//获取接口字段位置
		INTERFACE_FIELD_POSITION field_POSITION=(INTERFACE_FIELD_POSITION) BeanUtil.getBean(interfaceFieldInfo.getInterfaceFieldPosition());
		//通过字段获取值
		Object value = field_POSITION.getValue(interfaceFieldInfo, reqContext);
		return value;
	}
	/**
	 * 通过字段信息从请求参数获取值
	 * @param interfaceFieldInfo
	 * @param reqContext
	 * @return
	 */
	public static void setValue(InterfaceFieldInfo interfaceFieldInfo,Object value,ReqContext reqContext){
		if(interfaceFieldInfo.getInterfaceFieldInfo() != null){
			Object o = ParamsUtil.getValue(interfaceFieldInfo.getInterfaceFieldInfo(), reqContext);
			if(o == null){
				Object pValue=null;
				if(interfaceFieldInfo.getInterfaceFieldInfo().getInterfaceFieldType().equals("FIELD_TYPE_OBJECT")){
					pValue=new JSONObject();
				}
				if(interfaceFieldInfo.getInterfaceFieldInfo().getInterfaceFieldType().equals("FIELD_TYPE_ARRAY")){
					pValue=new JSONArray();
				}
				setValue(interfaceFieldInfo.getInterfaceFieldInfo(), pValue , reqContext);
			}
		}
		//获取接口字段位置
		INTERFACE_FIELD_POSITION_RES field_POSITION=(INTERFACE_FIELD_POSITION_RES) BeanUtil.getBean(interfaceFieldInfo.getInterfaceFieldPosition());
		//通过字段获取值
		field_POSITION.setValue(interfaceFieldInfo, value, reqContext);
	}
	/**
	 * 通过字段信息获取OGNL表达式
	 * @param interfaceFieldInfo
	 * @return
	 */
	public static String getPath(InterfaceFieldInfo interfaceFieldInfo){
		if(interfaceFieldInfo.getInterfaceFieldInfo() == null){
			return getRootPath(interfaceFieldInfo);
		}else{
			return getPath(interfaceFieldInfo.getInterfaceFieldInfo())+"."+interfaceFieldInfo.getInterfaceFieldName();
		}
	}
	public static String getRootPath(InterfaceFieldInfo interfaceFieldInfo){
		switch (interfaceFieldInfo.getInterfaceFieldPosition()) {
			case "FIELD_POSITION_REQ_URL_PATH":
				return "urlParams["+interfaceFieldInfo.getInterfaceFieldFormatting()+"]";
			case "FIELD_POSITION_REQ_HTTP_PARAMS":
				return "httpReqParams"+"."+interfaceFieldInfo.getInterfaceFieldName();
			case "FIELD_POSITION_REQ_PARAMS":
				return "reqParams"+"."+interfaceFieldInfo.getInterfaceFieldName();
			case "FIELD_POSITION_REQ_HTTP_HEAD":
				return "httpReqHead"+"."+interfaceFieldInfo.getInterfaceFieldName();
			case "FIELD_POSITION_RES_HTTP_HEAD":
				return "httpResHead"+"."+interfaceFieldInfo.getInterfaceFieldName();
			case "FIELD_POSITION_RES_HTTP_BODY":
				return "httpResParams"+"."+interfaceFieldInfo.getInterfaceFieldName();
			case "FIELD_POSITION_RES_PARAMS":
				return "resParams"+"."+interfaceFieldInfo.getInterfaceFieldName();
		}
		return null;
	}
	public static void main(String[] args) throws OgnlException {
		JSONObject jsonObject=new JSONObject();
			JSONObject IF1=new JSONObject();
				JSONObject IF1_IF1=new JSONObject();
					IF1_IF1.put("IF1_IF1_IF1", "value-IF1_IF1_IF1");
					IF1_IF1.put("IF1_IF1_IF2", "value-IF1_IF1_IF2");
				JSONObject IF1_IF2=new JSONObject();
				IF1_IF2.put("IF1_IF2_IF1", "value-IF1_IF2_IF1");

				IF1.put("IF1_IF1", IF1_IF1);
				IF1.put("IF1_IF2", IF1_IF2);
		jsonObject.put("IF1", IF1);
		
		System.out.println(JSON.toJSONString(jsonObject, true));
		
		ReqContext context=new ReqContext();
		context.setResParams(jsonObject);
		Ognl.setValue("resParams.IF1.IF1_TEST", context,"aa");

		System.out.println(Ognl.getValue("resParams.IF1.IF1_TEST", context));
	}
}
